<template>
  <div class="audioBox" @click="audioClick" :class="{my:sendType==1}">
    <audio
      ref="audio"
      @pause="onPause"
      @play="onPlay"
      @timeupdate="onTimeupdate"
      @loadedmetadata="onLoadedmetadata"
      :src="url"
      controls="controls"
      class="audio"
    ></audio>
    <!-- 语音时长 -->
    <span class="audioTime">{{audio.maxTime}}s</span>
    <!-- 语音图标 -->
    <div class="audioIconBox" :class="{rotate:sendType==1}">
      <div class="wifi-symbol">
        <div class="wifi-circle first"></div>
        <div
          class="wifi-circle second"
          :class="{ active: audio.playing }"
        ></div>
        <div
          class="wifi-circle third"
          :class="{ active: audio.playing }"
        ></div>
      </div>
    </div>
  </div>
</template>

<script>
// 将整数转换成 时：分：秒的格式
function realFormatSecond(second) {
  let secondType = typeof second;

  if (secondType === 'number' || secondType === 'string') {
    second = parseInt(second);

    let hours = Math.floor(second / 3600);
    second = second - hours * 3600;
    let mimute = Math.floor(second / 60);
    second = second - mimute * 60;

    return `${hours}:${`0${mimute}`.slice(-2)}:${`0${second}`.slice(-2)}`;
  }
  return '0:00:00';
}

/** vue */
import ChatMixin from '@src/modules/im/ChatMixin/index.js';

export default {
  name: 'audio-message',
  mixins:[ChatMixin],
  data() {
    return {
      audio: {
        // 该字段是音频是否处于播放状态的属性
        playing: false,
        // 音频当前播放时长
        currentTime: 0,
        // 音频最大播放时长
        maxTime: 0,
      },
    };
  },
  props: {
    voiceInfo: {
      type: Object,
      default: () => ({}),
    },
    sendType:{
      type: Number,
    },
  },
  computed:{
    url(){
      return this.compatibleFieldByExt(this.voiceInfo, 'url')
    }
  },
  created(){
    this.$eventBus.$on('stopVoice', this.pause)
  },
  methods: {
    // 控制音频的播放与暂停
    startPlayOrPause() {
      return this.audio.playing ? this.pause() : this.play();
    },
    // 播放音频
    play() {
      // 暂停其它正在播放的音频
      this.$eventBus.$emit('stopVoice')

      this.$refs.audio.play();
    },
    // 暂停音频
    pause() {
      this.$refs.audio.pause();
    },
    // 当音频播放
    onPlay() {
      this.audio.playing = true;
    },
    // 当音频暂停
    onPause() {
      this.audio.playing = false;
    },
    // 当timeupdate事件大概每秒一次，用来更新音频流的当前播放时间
    onTimeupdate(res) {
      this.audio.currentTime = res.target.currentTime;
    },
    // 当加载语音流元数据完成后，会触发该事件的回调函数
    // 语音元数据主要是语音的长度之类的数据
    onLoadedmetadata(res) {
      this.audio.maxTime = Math.ceil(res.target.duration);
    },
    // 播放暂停
    audioClick(){
      if(this.audio.maxTime == 0) return
      this.audio.playing ? this.pause() : this.play()
    }
  },
  filters: {
    // 使用组件过滤器来动态改变按钮的显示
    transPlayPause(value) {
      return value ? '暂停' : '播放';
    },
    // 将整数转化成时分秒
    formatSecond(second = 0) {
      return realFormatSecond(second);
    },
  },
  destroyed(){
    this.$eventBus.$off('stopVoice')
  }
};
</script>

<style scoped lang='scss'>
.audioBox {
  display: flex;
  align-items: center;
  height: 18px;
  overflow: hidden;
  cursor: pointer;
  &.my{
    flex-direction: row-reverse;
     .audioTime{
        margin-left: 8px;
        margin-right: 0;
      }
  }
  .audio {
    display: none;
  }
  .audioTime{
    margin-right: 8px;
  }
  .audioIconBox {
    box-sizing: border-box;
    position: relative;
    width: 30px;
    overflow: hidden;
    &.rotate{
      transform: rotate(180deg);
     }
  }
  .wifi-symbol {
    width: 50px;
    height: 50px;
    box-sizing: border-box;
    overflow: hidden;
    transform: rotate(135deg) scale(0.5);
  }
  .wifi-circle {
    border: 5px solid #999999;
    border-radius: 50%;
    position: absolute;
  }
  .first {
    width: 5px;
    height: 5px;
    background: #cccccc;
    top: 45px;
    left: 45px;
  }
  .second {
    width: 25px;
    height: 25px;
    top: 35px;
    left: 35px;
    &.active {
      animation: fadeInOut 1s infinite 0.2s;
    }
  }
  .third {
    width: 40px;
    height: 40px;
    top: 25px;
    left: 25px;
    &.active {
      animation: fadeInOut 1s infinite 0.4s;
    }
  }
}
@keyframes fadeInOut {
  0% {
    opacity: 0; /*初始状态 透明度为0*/
  }
  100% {
    opacity: 1; /*结尾状态 透明度为1*/
  }
}
</style>